<template>
  <el-row class="mb-1">
    <el-select v-model="PrinterName" placeholder="Select" style="width: 160px">
      <el-option :key="ds.id" :label="ds.value" :value="ds.value" v-for="ds in printerOptions"></el-option>
    </el-select>
    <el-button type="primary" class="ml10" @click="print">
      <el-icon>
        <Printer/>
      </el-icon>
      {{ $t('bar.print') }}
    </el-button>
  </el-row>
  <el-table :data="state.dataList"
            v-loading="state.loading"
            border
            style="width:100%;height: 100%"
            @selection-change="selectionChangeHandle"
            :cell-style="tableStyle.cellStyle"
            :header-cell-style="tableStyle.headerCellStyle"
            @sort-change="sortChangeHandle"
            ref="tableRef"
  >
    <el-table-column type="selection" width="40" align="center" show-overflow-tooltip/>
    <el-table-column prop="assetOrg" :label="$t('wms.assetOrg')" width="180" show-overflow-tooltip/>
    <el-table-column prop="shippersOrg" :label="$t('wms.shipperOrg')" width="180" show-overflow-tooltip/>
    <el-table-column prop="cardSource" :label="$t('wms.cardSource')" width="140" show-overflow-tooltip/>
    <el-table-column prop="assetClass" :label="$t('wms.assetClass')" width="140" show-overflow-tooltip/>
    <el-table-column prop="cardCode" :label="$t('wms.cardCode')" width="140" show-overflow-tooltip/>
    <el-table-column prop="assetName" :label="$t('wms.assetName')" width="240" show-overflow-tooltip/>
    <el-table-column prop="unit" :label="$t('iot.unit')" width="140" show-overflow-tooltip/>
    <el-table-column prop="assetsNumber" :label="$t('iot.qty')" width="140" show-overflow-tooltip>
      <template #default="scope">
        <el-tag effect="plain" type="warning">{{scope.row.assetsNumber}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="startDate" :label="$t('wms.startDate')" width="180" show-overflow-tooltip />
    <el-table-column prop="assetCode" :label="$t('wms.assetCode')" width="140" show-overflow-tooltip/>
    <el-table-column prop="barcode" :label="$t('qms.barCode')" width="140" show-overflow-tooltip/>
    <el-table-column prop="specification" :label="$t('qms.specification')" width="140" show-overflow-tooltip/>
    <el-table-column prop="assetLocation" :label="$t('wms.assetLocation')" width="240" show-overflow-tooltip/>
    <el-table-column prop="manufacturer" :label="$t('wms.manufacturer')" width="240" show-overflow-tooltip/>
    <el-table-column prop="documentStatus" :label="$t('mes.assetStatus')" width="140" show-overflow-tooltip>
      <template #default="scope">
        <el-tag effect="plain" type="primary">{{scope.row.documentStatus}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="dispositionSituation" :label="$t('wms.dispositionSituation')" width="140">
      <template #default="scope">
        <el-tag effect="plain" type="info">{{scope.row.dispositionSituation}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="availableQuantity" :label="$t('wms.quantityAvailable')" width="140">
      <template #default="scope">
        <el-tag  type="success">{{scope.row.availableQuantity}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="requisitionedQuantity" :label="$t('wms.quantityReceived')" width="140">
      <template #default="scope">
        <el-tag effect="plain" type="success">{{scope.row.requisitionedQuantity}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="initialization" :label="$t('wms.initialize')" width="140">
      <template #default="scope">
        <el-tag effect="plain" type="warning">{{scope.row.initialization}}</el-tag>
      </template>
    </el-table-column>

  </el-table>
  <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
  <location-form ref="locationFormRef" @refresh="getDataList(false)"></location-form>
</template>

<script setup lang="ts">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {getDetailsPageSet, printCard} from "/@/api/platform/digital/wmsAssetCard";
import {ref} from "vue";
import {getPrinter} from "/@/api/platform/wms/sp";
import {ElMessage} from "element-plus";
import {useI18n} from 'vue-i18n';

const printerOptions = ref();
const PrinterName = ref('');

const {t} = useI18n();

const LocationForm = defineAsyncComponent(() => import('./form.vue'));

// 搜索变量
const locationFormRef = ref()
const tableRef = ref();


// 初始化数据
onMounted(() => {
  getPrinter({code: "printerSetup", text: ""}).then((res) => {
    printerOptions.value = res.data;
  });
});
const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: getDetailsPageSet
})

const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  tableStyle,
  sortChangeHandle
} = useTable(state)

// 多选变量
const selectObjs = ref([]) as any

// 父级点击的行
const parentClickRow = {}


// 多选事件
const selectionChangeHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
};

// 搜索变量
const queryRef = ref()

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  getDataList()
}

// 通过id进行子表的查询
const query = (row: Object) => {
  // 赋值查询参数
  state.queryForm.assetCode = row.assetCode

  // 保存选中的行数据
  Object.assign(parentClickRow, row)
  // 刷新子表数据
  getDataList()
}

/**
 * 打印
 */
const print = () => {
  var selectedAry = tableRef.value.getSelectionRows();
  if (!PrinterName.value) {
    ElMessage.error(t('bar.selectPrinter'));
    return;
  }
  if (selectedAry.length == 0) {
    ElMessage.error(t('bar.selectBarcodes'));
    return;
  }
  printCard({"list": selectedAry, "printerName": PrinterName.value})

}

// 暴露变量
defineExpose({
  resetQuery,
  query
});

</script>

<style scoped lang="scss">

</style>